package com.lanyou.cook.entitylistener.domain;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.entity.kettle.KettleDirectory;

public class FieldSystemEntityListenerForKettleObject {

	// 保存和更新前，联动地更新R_DIRECTORY的信息
	
	@PrePersist
	public void prePersist(FieldSystem target) {
		setKettleDirectory(target);
	}
	
	@PreUpdate
	public void preUpdate(FieldSystem target) {
		setKettleDirectory(target);
	}
	
	public static void setKettleDirectory(FieldSystem target){
		KettleDirectory kettleDirectory = target.getKettleDirectory();
		if(kettleDirectory == null){
			kettleDirectory = new KettleDirectory();
			target.setKettleDirectory(kettleDirectory);
		}
		
		kettleDirectory.setName(target.getName());
		kettleDirectory.setParentId(target.getField().getKettleDirectory().getId());
	}
	
}
